package br.jcrsistemas.util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class NumberDocument extends PlainDocument {

	private static final long serialVersionUID = 1L;
	private int maxLength = 0;
	public NumberDocument() {
		super();
	}
	public NumberDocument(int tamanho) {
		super();
		maxLength = tamanho;
	}
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {

		if (str == null) {
			return;
		}
		for (int i = 0; i < str.length(); i++) {
			if (Character.isDigit(str.charAt(i)) == false) {
				return;
			}
		}
		if(getLength() + str.length() > maxLength ) {
			return;
		}
		super.insertString(offs, new String(str), a);
	}
}
